package com.ruoyi.system.domain.wrapper.view;

import cn.hutool.core.bean.BeanUtil;
import com.eic.framework.core.domain.BaseEntityWrapperV;
import com.ruoyi.common.core.domain.entity.DictData;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.dao.SysDictType;
import com.ruoyi.system.domain.view.SysDictTypeVo;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典类型前端实体包装
 *
 * @author 尹国春
 * @date 2022-07-22
 */
public class SysDictTypeWrapperV extends BaseEntityWrapperV<SysDictType, SysDictTypeVo> {

    private static SysDictTypeWrapperV wrapperV=null;
    //示例代码
    //private static IDemoDeptService demoDeptService;
    private SysDictTypeWrapperV(){
    }

    public static SysDictTypeWrapperV build(){
        if(wrapperV==null){
            //示例代码
            //demoDeptService= SpringUtils.getBean(IDemoDeptService.class);
            //Assert.notNull(demoDeptService);
            wrapperV=new SysDictTypeWrapperV();
        }
        return wrapperV;
    }

    @Override
    public SysDictTypeVo entityVO(SysDictType entity) {
        if(entity==null){
            return new SysDictTypeVo();
        }
        SysDictTypeVo vo = BeanUtil.copyProperties(entity,SysDictTypeVo.class);
        //示例代码
        //vo.setDeptName(getFieldValue(demoDeptService,entity.getDeptId(),DemoDept::getDepartName));
        return vo;
    }

    @Override
    public List<SysDictTypeVo> listVO(List<SysDictType> list) {
        List<SysDictTypeVo> voList=list.stream().map(type -> BeanUtil.copyProperties(type,SysDictTypeVo.class)).collect(Collectors.toList());
        for(SysDictTypeVo vo:voList){
            List<DictData> datas= DictUtils.getDictCache(vo.getDictType());
            List<String> fieldList=new ArrayList<>();
            for(DictData data:datas){
                fieldList.add(data.getDictValue()+":"+data.getDictLabel());
            }
            vo.setData(StringUtils.join(fieldList,","));
            vo.setStatusName(DictUtils.getDictLabel("sys_normal_disable",vo.getStatus()));
        }
        return voList;
    }
}